$(document).ready(function () {

});

//自定义登入业务操作对象
window.updatepassword = (function () {

    var obj = {};
    //处理用户登录操作
    obj.updatepassword = function () {
        var url = $("#updatepassword").attr("data-href");
        var data = {
            passwd: $("#passwd").val()
        };
        $.post(url, data, function (json) {
            window.modal.message("提示", json.message);
        }, "json");
    };
    return obj;
})();

//自定义表单验证操作对象
window.validate1 = (function () {
    var obj = {};
    //o为表单项，option为验证类型，data为验证相关参数
    obj.valid = function (o, option, data) {
        var flag = false;//是否有错
        var msg = "";//错误消息

        $(option).each(function (i) {
            switch (option[i]) {
                case "require":
                    var reg = /(^\s+)|(\s+$)/g;
                    var value = $(o).val();
                    if (value.replace(/^\s+/, '').replace(/\s+$/, '') == "") {
                        msg += "该项为必填项，请输入";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    } else if (reg.test(value)) {
                        msg += "输入的内容前后不允许有空格";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    break;
                case "oldpasswd":
                    var value = $(o).val();
                    //远程验证
                    if (flag == false && typeof data[i].url != "undefined") {
                        var url = data[i].url;

                        $.ajax({
                            url: url,
                            async: false,
                            data: {"oldpasswd": oldpasswd},
                            dataType: 'json',
                            success: function (json) {
                                if (json.success == false) {
                                    flag = true;
                                    msg += json.message;
                                    obj.errorCSS(o, msg);
                                    $(o).attr("valid", false);//验证状态是否为有效
                                }
                            }
                        });
                    }
                    break;
                case "passwd":
                    var passwd = $(o).val();
                    var re = new RegExp("[a-zA-Z]");
                    var len = re.test(passwd);
                    re = new RegExp("[0-9]");
                    len = re.test(passwd);
                    re = new RegExp("((?=[\x21-\x7e]+)[^A-Za-z0-9])");
                    len = re.test(passwd);
                    if (!len || (passwd.length < 8) || (passwd.length > 20)) {
                        msg += "密码必须包含字母数字符号，8-20位";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    break;
                case "repasswd":
                    var first = $("#" + data[i].id).val();
                    var second = $(o).val();
                    if (first != second) {
                        msg += "输入的密码不一致，请重新输入";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    break;
                default:
                    return obj;
            }
        })
    }
});


$("#updatepassword").on("click", function () {
    window.updatepassword.updatepassword();
});

//自定义通用提示消息模态框
window.modal = (function () {
    var obj = {};
    obj.message = function (title, msg) {
        $('#myModal').on('show.bs.modal', function () {
            var modal = $(this);
            modal.find('.modal-title').html(title);
            modal.find('.modal-body').html(msg);
        });
        $('#myModal').modal({backdrop: 'static', keyboard: false});
    };
    return obj;
})();
